package me.cunity.ui;

import flash.display.DisplayObject;
import flash.xml.XML;
import haxe.FastList;
import haxe.Stack;
import me.cunity.core.Application;
import me.cunity.debug.Out;
import me.cunity.effects.STween;
import me.cunity.events.ResourceEvent;
import me.cunity.ui.BaseCell;
import me.cunity.ui.Container;

/**
 * ...
 * @author Axel Huizinga axel@cunity.me
 */

class Overlay extends Container
{
	
	public function new(xN:XML, p:BaseCell) 
	{
		resourceList = new FastList<DisplayObject>();
		_parent = p.layoutRoot;
		//getBox();
		super(xN, p.layoutRoot);
		p.layoutRoot.addChild(this);
		_alpha = alpha;
		alpha = 0;
		//box = _parent.box.clone();
		trace (resourceList.isEmpty() + ' added2:' + p.name);
		if (resourceList.isEmpty())
			layout();
		else
			addEventListener(ResourceEvent.RESOURCES_COMPLETE, wait4children,false,0,true);
	}
	
	override public function addChildren()
	{
		getBox();
		super.addChildren();		
	}
	
	override public function layout()
	{
		//getBox();
		super.layout();
		//box = Application.instance.frame.actScreen.box.clone();
		/*switch(cAtts.pack)
		{
			case 'CM'://CENTERMIDDLE
			x = (_parent.box.width - box.width) / 2;
			y = (_parent.box.height - box.height) / 2;
			default:
			throw('not implemented :-(');
		}*/
		if (bG != null)
			bG.select(xNode.elements('BackGround')[0].attribute('id').toString());
		visible = true;
		fadeIn();
		trace(this + ':' + box);
		try {
			Out.dumpLayout(this);
		}
		catch (ex:Dynamic)
		{
			Out.dumpStack(Stack.callStack());
		}
	}
	
	function close()
	{
		parent.removeChild(this);
		destroy();
	}
	
}